﻿using locationModule.ServiceReference1;
using locationModule.ViewModels;
using Microsoft.Practices.Prism.Mvvm;
using ReactiveUI;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.Composition;
using System.Data.Services.Client;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace locationModule.Views
{
    /// <summary>
    /// Interaction logic for AddModule3MainView.xaml
    /// </summary>
    [Export("AddModule3MainView")]
    public partial class AddModule3MainView : UserControl,IView
    {
        Container context = new ServiceReference1.Container(new Uri("http://172.16.1.12:11245/odata"));
        
            
        public AddModule3MainView()
        {
            
            InitializeComponent();      
             if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
             {
             	//여기에 데이터를 로드하고 결과를 CollectionViewSource에 할당하십시오.
                 System.Windows.Data.CollectionViewSource location_tableViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["location_tableViewSource"];
                 location_tableViewSource.Source = context.Location_table;
             }
            
            //-------------------bind
            //this.Bind(ViewModel, vm => vm.Lotable.location, v => v.locationTextBox.Text);
            //this.Bind(ViewModel, vm => vm.Lotable.location_set, v => v.location_setTextBox.Text);
            //this.Bind(ViewModel, vm => vm.Lotable.location_sub, v => v.location_subTextBox.Text);
            //this.Bind(ViewModel, vm => vm.Lotable.complete_predict, v => v.complete_predictTextBox.Text);
            //this.Bind(ViewModel, vm => vm.Lotable.set_predict, v => v.set_predictTextBox.Text);

                          //-------------------bind end

                          //-------------------bindcommand
                          //this.BindCommand(ViewModel, vm => vm.Insert, v => v.Insert);
                          //-------------------bindcommandend
        }

        private void Insert_Click(object sender, RoutedEventArgs e)
        {
            try {
                if (this.complete_predictTextBox.Text != null)
                {


                    Location_table insertLocation = new Location_table
                    {
                        complete_predict = Convert.ToInt32(this.complete_predictTextBox.Text),
                        location = this.locationTextBox.Text,
                        location_set = this.location_setTextBox.Text,
                        location_sub = this.location_subTextBox.Text,
                        set_predict = Convert.ToInt32(this.set_predictTextBox.Text)
                    };

                    //context.AddObject("Location_table", insertLocation);
                    context.AddToLocation_table(insertLocation);

                    //context.BeginSaveChanges(AsynchCallback, insertLocation);
                    context.SaveChanges();

                    DataServiceCollection<Location_table> lstData = new DataServiceCollection<Location_table>();
                    lstData.LoadCompleted += (senderobj, evtargs) =>
                     {
                         if (evtargs.Error == null)
                         {
                         location_tableDataGrid.ItemsSource = lstData;
                         }
                     };

                     lstData.LoadAsync(context.Location_table);

                    MessageBox.Show(locationTextBox.Text + "(이)가 입력되었습니다.", "입력완료 메세지");

                    TextInit();


                    //location_tableDataGrid.DataContext = context.Location_table;

                }
                else
                {
                    MessageBox.Show("입력하시오");
                }
            }
            catch (DataServiceRequestException dataServiceRequestException)
            {
                MessageBox.Show("같은 값이 있네요~");
            }
            catch (FormatException formatException)
            {
                MessageBox.Show("값을 입력하시오");
            }

        }

        private void Delete_Click(object sender, RoutedEventArgs e)
        {

                try
                {
                    //var idx = this.location_tableDataGrid.SelectedIndex + 1;
                    if (this.locationTextBox.Text != null)
                    {
                        var delete = this.locationTextBox.Text;

                        Location_table selectItem = context.Location_table
                                                    .Where(lo => lo.location == delete)
                                                    .SingleOrDefault();


                        if (selectItem != null)
                        {
                            context.DeleteObject(selectItem);
                            context.SaveChanges();

                            DataServiceCollection<Location_table> lstData = new DataServiceCollection<Location_table>();
                            lstData.LoadCompleted += (senderobj, evtargs) =>
                            {
                                if (evtargs.Error == null)
                                {
                                    location_tableDataGrid.ItemsSource = lstData;
                                }
                            };

                            lstData.LoadAsync(context.Location_table);

                            MessageBox.Show(selectItem.location + "(이)가 삭제되었습니다.", "삭제완료 메세지");

                            TextInit();

                        }
                        else
                        {
                            MessageBox.Show("데이타가 없네요. 지울 데이터를 입력하시오");
                        }
                    }
                    else
                    {
                        MessageBox.Show("지울 공장 data를 입력하시오");
                        this.locationTextBox.Focus();
                    }
                }
                catch (DataServiceQueryException d)
                {
                    MessageBox.Show("삭제할 공장 Data를 선택하세요");
                }
                catch (DataServiceRequestException dre)
                {
                    MessageBox.Show("다른 항목에서 참조중인 data는 삭제 할 수 없습니다.");
                 }


        }

        private void select_Click(object sender, RoutedEventArgs e)
        {
            Location_table loca = this.location_tableDataGrid.SelectedItem as Location_table;

            this.locationTextBox.Text = loca.location;
            this.location_subTextBox.Text = loca.location_sub;
            this.location_setTextBox.Text = loca.location_set;
            this.complete_predictTextBox.Text = loca.complete_predict.ToString();
            this.set_predictTextBox.Text = loca.set_predict.ToString();

        }

        private void Update_Click(object sender, RoutedEventArgs e)
        {
            if (this.locationTextBox.Text != null)
            {
                var update = this.locationTextBox.Text;

                //MessageBox.Show(delete);
                Location_table selectItem = context.Location_table
                                            .Where(lo => lo.location == update)
                                            .SingleOrDefault<Location_table>();

                selectItem.complete_predict = Convert.ToInt32(this.complete_predictTextBox.Text);
                selectItem.location_set = this.location_setTextBox.Text;
                selectItem.location_sub = this.location_subTextBox.Text;
                selectItem.set_predict = Convert.ToInt32( this.set_predictTextBox.Text);
                


                if (selectItem != null)
                {
                    context.UpdateObject(selectItem);
                    context.SaveChanges();

                    DataServiceCollection<Location_table> lstData = new DataServiceCollection<Location_table>();
                    lstData.LoadCompleted += (senderobj, evtargs) =>
                    {
                        if (evtargs.Error == null)
                        {
                            location_tableDataGrid.ItemsSource = lstData;
                        }
                    };

                    lstData.LoadAsync(context.Location_table);

                    MessageBox.Show(selectItem.location + "의 수정이 완료 되었습니다.", "수정완료 메세지");

                    TextInit();

                }
            }
        }

        public void TextInit()
        {
            this.locationTextBox.Text = "";
            this.location_subTextBox.Text = "";
            this.location_setTextBox.Text = "";
            this.complete_predictTextBox.Text = "";
            this.set_predictTextBox.Text = "";

        }
    }
}
